﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum ItemType
{
    Normal,
    Obstacle,//障碍物
    Start,//起点
    End//终点
}

public class GridItem : MonoBehaviour,IComparable<GridItem>
{
    //用于存放在数组的下标
    public int x;
    public int y;
    //格子类型
    public ItemType itemType;
    //F = G + H
    public int F;
    public int G;
    public int H;

    //发现者
    public GridItem parent;
    
    private MeshRenderer _meshRenderer;

    private void Awake()
    {
        //找到渲染器
        _meshRenderer = GetComponent<MeshRenderer>();
    }

    /// <summary>
    /// 设置格子颜色
    /// </summary>
    /// <param name="color"></param>
    public void SetColor(Color color)
    {
        _meshRenderer.material.color = color;
    }

    /// <summary>
    /// 设置格子类型
    /// </summary>
    /// <param name="itemType"></param>
    public void SetItemType(ItemType itemType)
    {
        this.itemType = itemType;

        switch (itemType)
        {
            case ItemType.Obstacle :
                _meshRenderer.material.color = Color.blue;
                break;
            case ItemType.Start :
                _meshRenderer.material.color = Color.red;
                break;
            case ItemType.End :
                _meshRenderer.material.color = Color.green;
                break;
        }
    }
    
    /// <summary>
    /// 根据F值从小到大排序格子在List中位置
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    public int CompareTo(GridItem other)
    {
        if (F < other.F)
        {
            //当前格子在List中的下标-1
            return -1;
        }
        if (F > other.F)
        {
            //当前格子在List中的下标+1
            return 1;
        }
        //当前格子在List中的下标+0
        return 0;
    }
}